package com.esd.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.esd.config.BaseConfig;
import com.esd.csair.Csair;
import com.esd.csair.CsairImpl;
import com.esd.util.ByteToObject;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;

/**
 * 用户登陆过滤器
 * 
 * @author liukaiK
 * 
 */
public class LoginInterceptor implements HandlerInterceptor {
	private static  Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {

	}

	public void postHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) {
		// 添加南航交互类
		if (request.getRequestURI().indexOf("/cs") != -1) { //拦截南航项目
			HttpSession session = request.getSession();
			if (session != null) {
				Object obj = session.getAttribute(Csair.WEBCLIENT);
				if (obj == null) {//如果模拟浏览器为空 跳转到主页
					try {
						response.setContentType("text/html");
						response.setCharacterEncoding("utf-8");
						PrintWriter out = response.getWriter();
						StringBuilder builder = new StringBuilder();
						builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
						//builder.append("alert(\"请重新登陆！\");");
						builder.append("window.location.href='index?"+System.currentTimeMillis()+"'");
						builder.append("</script>");
						out.print(builder.toString());
						out.close();
						session.setAttribute(Csair.WEBCLIENT, new CsairImpl());
						return false;
					} catch (IOException e) {
						log.error("IOException", e);
					}
				}else {//如果模拟浏览器不为空
					CsairImpl csair=(CsairImpl)session.getAttribute(Csair.WEBCLIENT);
					WebClient webClient= (WebClient)csair.getWebClient();
					WebClientOptions opts=webClient.getOptions();
//					System.out.println("***********************");
//					System.out.println(opts.isRedirectEnabled());
//					System.out.println(opts.getMaxInMemory());
//					System.out.println("***********************");
		
					
				}
			}

		}
		if (request.getRequestURI().indexOf("/admin") != -1) {
			Object obj = request.getSession().getAttribute(BaseConfig.USER);
			if (obj == null) {
				try {
					response.setContentType("text/html");
					response.setCharacterEncoding("utf-8");
					PrintWriter out = response.getWriter();
					StringBuilder builder = new StringBuilder();
					builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
					builder.append("alert(\"请重新登陆！\");");
					builder.append("window.location.href=\"/iac/login\"");
					builder.append("</script>");
					out.print(builder.toString());
					out.close();
					return false;
				} catch (IOException e) {
					log.error("IOException", e);
				}
			}
		}
		return true;
	}
}
